package com.w3coffee.web.control;

import java.util.Collection;
import java.util.Comparator;
import java.util.concurrent.ConcurrentSkipListMap;

import com.w3coffee.web.dao.CoffeeDao;
import com.w3coffee.web.dao.model.TNews;

public class NewsManager {
	private ConcurrentSkipListMap<Integer, TNews> news;
	private CoffeeDao dao;
	private int count;
	
	public NewsManager(CoffeeDao dao, int count) throws Exception {
		this.dao = dao;
		this.count = count;
		
		news = new ConcurrentSkipListMap<Integer, TNews>(new Comparator<Integer>() {
			public int compare(Integer id1, Integer id2) {
				// News按照ID倒序排列，约新的在越前面
				return id2 - id1;
			}});
		
		for (TNews rec : dao.getNews(count)) {
			news.put(rec.getId(), rec);
		}
	}
	
	public Collection<TNews> getNews() {
		return news.values();
	}
	
	public TNews getNews(int id) {
		return news.get(id);
	}
}
